package edu.whu.demo.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import edu.whu.demo.dao.EntryDao;
import edu.whu.demo.domain.Entry;
import edu.whu.demo.domain.Message;
import edu.whu.demo.dao.MessageDao;
import edu.whu.demo.domain.MessageDTO;
import edu.whu.demo.exception.MessageException;
import edu.whu.demo.service.IMessageService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author
 * @since 2022-12-15
 */
@Service
public class MessageServiceImpl extends ServiceImpl<MessageDao, Message> implements IMessageService {

    @Autowired
    EntryDao entryDao;

    @Override
    public IPage<MessageDTO> getOuterMessage(String type,
                                        Long toUid,
                                        Integer pageNum,
                                        Integer pageSize)
    {
        Page<MessageDTO> page = new Page<>(pageNum, pageSize);
        return this.baseMapper.getOuterMessage(page, type, toUid);
    }

    @Override
    public IPage<MessageDTO> getMessage(Map<String, Object> condition,
                                 Integer pageNum,
                                 Integer pageSize)
    {
        Page<MessageDTO> page = new Page<>(pageNum, pageSize);
        QueryWrapper<MessageDTO> qw = new QueryWrapper<>();
        qw.eq(condition.containsKey("type"), "type", condition.get("type"));
        qw.eq(condition.containsKey("toUid"), "to_uid", condition.get("toUid"));
        qw.eq(condition.containsKey("activityId"), "activity_id", condition.get("activityId"));
        qw.eq(condition.containsKey("isRead"), "is_read", condition.get("isRead"));
        return this.baseMapper.getMessage(page, qw);

    }

    @Override
    public void setRead(Map<String, Object> condition) throws MessageException {
        QueryWrapper<Message> qw = new QueryWrapper<>();
        qw.eq(condition.containsKey("id"),"id",condition.get("id"));
        qw.eq(condition.containsKey("type"), "type", condition.get("type"));
        qw.eq(condition.containsKey("toUid"), "to_uid", condition.get("toUid"));
        qw.eq(condition.containsKey("activityId"), "activity_id", condition.get("activityId"));
        qw.eq( "is_read", 0);
        List<Message> updateList = this.baseMapper.selectList(qw);
        try
        {
            for (Message message: updateList)
            {
                message.setIsRead(1);
                this.updateById(message);
            }
        }
        catch (Exception e)
        {
            throw new MessageException("消息更新失败："+e.getMessage());
        }

    }

    @Override
    public Message addMessage(Message message) throws MessageException
    {
        //对活动变化提醒或者活动消息提醒，找到每一个关注者的用户id
        if ("change".equals(message.getType()) || "reminder".equals(message.getType()))
        {
            QueryWrapper<Entry> qw = new QueryWrapper<>();
            qw.eq("activity_id", message.getActivityId());
            List<Entry> addList = entryDao.selectList(qw);
            try
            {
                for (Entry entry: addList)
                {
                    message.setToUid(entry.getUid());
                    this.baseMapper.insert(message);
                }
            }
            catch (Exception e)
            {
                throw new MessageException("消息添加失败："+e.getMessage());
            }
            return message;
        }
        //评论消息和审核消息都是直接插入
        else
        {
            try
            {
                this.baseMapper.insert(message);
            }
            catch (Exception e)
            {
                throw new MessageException("消息添加失败："+e.getMessage());
            }
            return message;
        }
    }

    @Override
    public void deleteMessage(Message message) throws MessageException
    {
        if ("reminder".equals(message.getType()))
        {
            QueryWrapper<Message> qw = new QueryWrapper<>();
            qw.eq("from_uid", message.getFromUid())
                    .eq("reminder", message.getType());
            try
            {
                this.baseMapper.delete(qw);
            }
            catch (Exception e)
            {
                throw new MessageException("消息删除失败："+e.getMessage());
            }
        }
        else
        {
            try
            {
                this.baseMapper.deleteById(message.getId());
            }
            catch (Exception e)
            {
                throw new MessageException("消息删除失败："+e.getMessage());
            }
        }
    }


    @Override
    public void updateMessage(Message message) throws MessageException
    {
        if ("reminder".equals(message.getType()))
        {
            QueryWrapper<Message> qw = new QueryWrapper<>();
            qw.eq("from_uid", message.getFromUid())
                    .eq("reminder", message.getType());
            List<Message> updateList =  this.baseMapper.selectList(qw);
            try
            {
                for (Message messageToUpdate: updateList)
                {
                    messageToUpdate.setTime(message.getTime());
                    messageToUpdate.setContent(message.getContent());
                    this.baseMapper.updateById(message);
                }
            }
            catch (Exception e)
            {
                throw new MessageException("消息内容更新失败："+e.getMessage());
            }
        }
        else if ("comment".equals(message.getType()))
        {
            QueryWrapper<Message> qw = new QueryWrapper<>();
            qw.eq("from_uid", message.getFromUid())
                    .eq("comment", message.getType());
        //    要做这个必须给message加comment_id
        }
        else
        {
            try
            {
                this.baseMapper.updateById(message);
            }
            catch (Exception e)
            {
                throw new MessageException("消息内容更新失败："+e.getMessage());
            }
        }
    }


}
